package com.quanxi.qxaimain.common.swagger;

import com.quanxi.qxaimain.common.message.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @Author lyx
 * @create 2021/9/16 16:23
 */

@Component
public class SwaggerStartConfig implements ApplicationRunner {
    private static final Logger LOG = LoggerFactory.getLogger(SwaggerStartConfig.class);

    @Autowired
    private ServerConfig serverConfig;

    @Override
    public void run(ApplicationArguments args){
     //   先判断当前环境
        if (!exitisVersion()){
            //启动swagger-ui
            application();
        }
       // application();
    }

    private Boolean exitisVersion() {
        String osName = System.getProperties().getProperty("os.name");
        if(osName.equals("Linux")) {
            LOG.info("running in Linux successfully！！！ ");
            LOG.info("explorer %s/qxzsk/docs.html", serverConfig.getUrl());
            return true;
        }
        else{
          //  LOG.info("don't running in Linux");
            return false;
        }
    }

    private void application() {

            String cmd = String.format("explorer %s/qxzsk/docs.html", serverConfig.getUrl());
            LOG.info("项目地址：" + cmd);
            Runtime run = Runtime.getRuntime();
            try{
                run.exec(cmd);
                LOG.info("启动成功");
            }catch (Exception e){
                e.printStackTrace();
                LOG.error(e.getMessage());
            }
        }

}
